package org.one.stone.soup.grfxML.plugin.builder;

import org.one.stone.soup.grfxML.DataChildren;
import org.one.stone.soup.grfxML.DataNode;
import org.one.stone.soup.grfxML.DataState;
import org.one.stone.soup.grfxML.DataString;
import org.one.stone.soup.grfxML.GrfxMLEngine;
import org.one.stone.soup.grfxML.plugin.SimplePlugin;
import org.one.stone.soup.grfxML.plugin.grfxMLCaster;
import org.one.stone.soup.mjdb.data.field.DataLinker;
import org.one.stone.soup.mjdb.data.field.Field;
import org.one.stone.soup.mjdb.data.field.LockException;
import org.one.stone.soup.stringhelper.StringGenerator;

public class PrototypeBuilder extends SimplePlugin {

	public static final int ARG_BUILD = 0;

	public static final int ARG_PROTOTYPENAME = 1;

	public static final int ARG_TARGET = 2;

	public static final int ARG_I_PROTOTYPENAME = 3;

	public static final int ARG_I_TARGET = 4;


	org.one.stone.soup.grfxML.DataState build = new DataState();

	DataString prototypeName = new DataString();
	DataChildren target;

	DataString i_prototypeName;
	DataChildren i_target;
/**
 * NodeBuilder constructor comment.
 */
public PrototypeBuilder(GrfxMLEngine engine){
	super(engine);

	target = new DataChildren(getEngine());
	i_target = new DataChildren(getEngine());
}
/**
 * initialize method comment.
 */
public void initialize() {}
/**
 * process method comment.
 */
public void process()
{
 try{
	if(build.getValue()==true)
	{
		System.out.println("built it");
		build.setValue( false,this );

		getEngine().getAPI().setAutoCommit(false);

		String idNo = StringGenerator.generateUniqueId();

		DataNode newNode = getEngine().getAPI().buildPrototype(
								prototypeName.getValue(),
								"build"+idNo,
								target
							);

		if( i_prototypeName.getValue().length()>0 )
		{
			DataNode new_i_Node = getEngine().getAPI().buildPrototype(
									i_prototypeName.getValue(),
									"build"+idNo,
									i_target
								);
			new_i_Node.setId("I_build"+idNo,this);
		}

		getEngine().getAPI().commit();

		nodeAdded(newNode);
	}
 }catch(LockException le){}
}
public void nodeAdded(DataNode newNode)
{
	// For use by subclasses
}
/**
 * register method comment.
 */
public void register(DataLinker store)
{

	build = grfxMLCaster.cast(build,getArg(build,ARG_BUILD,store));

	prototypeName = grfxMLCaster.cast(prototypeName,getArg(prototypeName,ARG_PROTOTYPENAME,store));

	target = grfxMLCaster.cast(target,getArg(target,ARG_TARGET,store));

	i_prototypeName = grfxMLCaster.cast(i_prototypeName,getArg(i_prototypeName,ARG_I_PROTOTYPENAME,store));

	i_target = grfxMLCaster.cast(i_target,getArg(i_target,ARG_I_TARGET,store));
}
/**
 * replace method comment.
 */
public void replace(Field oldObj, Field newObj)
{

	build = grfxMLCaster.replace(build,oldObj,newObj);

	prototypeName = grfxMLCaster.replace(prototypeName,oldObj,newObj);

	target = grfxMLCaster.replace(target,oldObj,newObj);

	i_prototypeName = grfxMLCaster.replace(i_prototypeName,oldObj,newObj);

	i_target = grfxMLCaster.replace(i_target,oldObj,newObj);
}
/**
 * stop method comment.
 */
public void stop() {}
}
